package com.example.t1;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;

import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toolbar;

import com.example.t1.ui.main.SectionsPagerAdapter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class MainActivity extends AppCompatActivity implements GameFragment.ImageClickedListener{

    Chessboard chessboard = new Chessboard();
    GameFragment gameFragment;
    Intent intent = null;
    SharedPreferences prefs ;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gameFragment = new GameFragment();

        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(gameFragment,this, getSupportFragmentManager());
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);

        try {
            FileInputStream fileInputStream = openFileInput("config");
            byte[] input = new byte[fileInputStream.available()];
            fileInputStream.read(input);
            String str[] = new String(input).split("#");

            Chessboard.name = str[0];
            Chessboard.photo = Integer.parseInt(str[1]);
            Log.i("sd",Chessboard.photo+"");
        } catch (Exception e) {
            e.printStackTrace();
        }

        TextView textView =(TextView) findViewById(R.id.title);
        registerForContextMenu(textView);

        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        boolean f=prefs.getBoolean("bgm",true);
        intent = new Intent(this,MyService.class);
        if(f)
            startService(intent);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_1,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    public void myOnclick(MenuItem menuItem){
        chessboard.reset();
        gameFragment.repaint();
    }

    public void myOnclick1(MenuItem menuItem){
        chessboard.setNandu(10);
        gameFragment.repaint();
    }
    public void myOnclick2(MenuItem menuItem){
        chessboard.setNandu(15);
        gameFragment.repaint();
    }
    public void myOnclick3(MenuItem menuItem){
        chessboard.setNandu(25);
        gameFragment.repaint();
    }


    int tx = -1 , ty = -1;
    @Override
    public void removeimg(ImageView img) {
        intent = new Intent(this,MyIntentService.class);
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        boolean f = prefs.getBoolean("click",true);
        int id =img.getId();
        int y=id%10;
        int x=id/10;
        if(tx==-1){
            tx = x;
            ty = y;
            intent.putExtra("type","cl");
        }
        else{
            if(chessboard.check(tx,ty,x,y)){
                intent.putExtra("type","ac");
                chessboard.setDis(tx,ty);
                chessboard.setDis(x,y);
            }
            else if(tx==x&&ty==y){
                intent.putExtra("type","cl");
            }
            else{
                intent.putExtra("type","wa");
            }
            tx = -1;
            ty = -1;
        }
        if (f)
            startService(intent);
    }
}